package com.hjb.service.impl.user;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hjb.service.config.constant.Constants;
import com.hjb.service.domain.siteuser.SiteUserCurrent;
import com.hjb.service.domain.siteuser.SiteUserCurrentExample;
import com.hjb.service.mapper.SiteUserAccountMapper;
import com.hjb.service.mapper.SiteUserCurrentMapper;
import com.hjb.service.user.SiteUserCurrentService;
import com.hjb.service.utils.HJBUT;

/**
 * 活期购买金额转持有接口业务实现类
 * @author zhangxl
 * @date 2017-07-20
 * @version 1.0
 */
@Service
public class SiteUserCurrentServiceImpl implements SiteUserCurrentService {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private SiteUserCurrentMapper siteUserCurrentMapper;
	@Autowired
	private SiteUserAccountMapper siteUserAccountMapper;
	
	@Override
	public BigDecimal queryTotalCurrentByUserId(String userId) {
		return siteUserCurrentMapper.queryTotalCurrentByUserId(userId);
	}

	@Override
	@Transactional
	public void transferToCurrentAccount() {
		try {
			log.info("用户活期购买成功，转入持有金额 定时任务开始：......................."+HJBUT.currentDate());
			//查询今日待转计划
			SiteUserCurrentExample userCurrentExample = new SiteUserCurrentExample();
			userCurrentExample.createCriteria().andInterestDateEqualTo(HJBUT.currentDate()).andStatusEqualTo(Constants.COMMON_STATUS_0);
			List<SiteUserCurrent> userCurrentList = siteUserCurrentMapper.selectByExample(userCurrentExample);
			log.info("用户活期购买成功，转入持有金额 定时任务开始：共计任务数："+userCurrentList.size());
			for(SiteUserCurrent voUserCurrent:userCurrentList){
				log.info("用户活期购买成功，转入持有金额 ：......................."+voUserCurrent.getAmount()+",user_id:"+voUserCurrent.getUserId());
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", voUserCurrent.getUserId());
				params.put("amount", voUserCurrent.getAmount().negate());
				siteUserAccountMapper.updateCurrentBalance(params);
				
				SiteUserCurrent updateUserCurrent = new SiteUserCurrent();
				updateUserCurrent.setId(voUserCurrent.getId());
				updateUserCurrent.setStatus(Constants.COMMON_STATUS_1);
				updateUserCurrent.setUpdateTime(new Date());
				siteUserCurrentMapper.updateByPrimaryKeySelective(updateUserCurrent);
			}
			
			log.info("用户活期购买成功，转入持有金额 定时任务结束：......................."+HJBUT.currentDate());
			
		} catch (Exception e) {
			log.info("用户活期购买成功，转入持有金额 定时任务异常：......................."+HJBUT.currentDate());
		}
		
	}

}
